/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.scos.CampaignSearchCondition;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import com.insurance.cis.views.StatisticsReportJPanelView;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TruongLQ
 */
public class StatisticsReportController {
    
    final StatisticsReportJPanelView statisticsReportView;
    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);

    public StatisticsReportController(StatisticsReportJPanelView jPanelView) {
        this.statisticsReportView = jPanelView;
    }
    
    public CampaignSearchCondition validateSearchConditions() {
        Date startDate;
        try {
            startDate = validateDate(statisticsReportView.getTxtStartDate().getText().trim());
        } catch (ParseException ex) {
            statisticsReportView.getTxtStartDate().requestFocus();
            MessageDialogUtils.dialogError(statisticsReportView, "Start date must be in format dd/mm/yyyy!");
            return null;
        }
        Date endDate;
        try {
            endDate = validateDate(statisticsReportView.getTxtEndDate().getText().trim());
        } catch (ParseException ex) {
            statisticsReportView.getTxtEndDate().requestFocus();
            MessageDialogUtils.dialogError(statisticsReportView, "End date must be in format dd/mm/yyyy!");
            return null;
        }
        
        if (startDate!=null && endDate !=null && startDate.after(endDate)) {
            MessageDialogUtils.dialogError(statisticsReportView, "Start date must be <= end date!");
            statisticsReportView.getTxtStartDate().requestFocus();
            return null;
        }
        return new CampaignSearchCondition(statisticsReportView.getTxtCampaignID().getText().trim(),
                "", statisticsReportView.getTxtProductID().getText().trim(),
                null, startDate, endDate); 
    }
    
    private Date validateDate(String date) throws ParseException {
        Date dateRet = null;

        if (!date.isEmpty()) {
            if (date.trim().length()!=10) {
                throw new ParseException("Date parse exception", 0);
            }
            Matcher matcher = pattern.matcher(date.trim());
            if (!matcher.matches()) {
                throw new ParseException("Date parse exception", 0);
            }
            dateRet = Utils.parseDate(date.trim(),
                    CisConstants.DATE_DD_MM_YYYY);
        }
        return dateRet;
    }
    
    public boolean isOkReport() {
        if (!statisticsReportView.getRbtnModeAll().isSelected()
                && !statisticsReportView.getRbtnModeSelected().isSelected()) {
            MessageDialogUtils.dialogError(statisticsReportView, "You have to choose the mode to export!");
            return false;
        } 
        if (!statisticsReportView.getRbtnDifferentCampaigns().isSelected()
                && !statisticsReportView.getRbtnVariances().isSelected()
                && !statisticsReportView.getRbtnSalesTarget().isSelected()
                && !statisticsReportView.getRbtnDefault().isSelected()) {
            MessageDialogUtils.dialogError(statisticsReportView, "You have to choose the data to export!");
            return false;
        }
        return true;
    }
}
